package cn.edu.nju.ws.sview.wyge.relations.adataset;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;

import com.hp.hpl.jena.db.impl.PSet_ReifStore_RDB;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.cache.URILabelCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.views.CustomCollection;
import cn.edu.nju.ws.sview.views.CustomEntity;
import cn.edu.nju.ws.sview.views.Entity;
import cn.edu.nju.ws.sview.views.Group;
import cn.edu.nju.ws.sview.views.User;
import cn.edu.nju.ws.sview.views.data.EntityManagement.URIMember;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetBrowsing;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetName;

/**
 * 
 * @author Weiyi Ge
 */
public class CustomDataset {
	private String name = null;
	private int id = -1;
	private String lang = null;
//	private String author = null;
	private String group = null;//group name
	private HashSet<String> urls = null;
	private Date createdDate = null;
	private int docNum = -1;

//	public CustomDataset(int id, String name, String author, String lang,
//			HashSet<String> urls, Date date, int docNum) {
	public CustomDataset(int id, String name, String group, String lang,
			HashSet<String> urls, Date date, int docNum) {
		this.name = name;
		this.group = group;
		this.id = id;
		this.lang = lang;
		this.urls = urls;
		this.createdDate = date;
	}
	public CustomDataset(String name, String group, String lang, HashSet<String> urls){
		this.name = name;
		this.group = group;
		this.lang = lang;
		this.urls = urls;
	}
	/**
	 * update name or translate
	 * @param datasetId
	 * @param groupID
	 * @param newName
	 * @param lang
	 * @return
	 */
	public static boolean addOrUpdateLangName(int datasetId, String newName, String lang){
//		int checkId = getCustomDatasetIdByNameGroup(newName, groupID,lang);
//		if(checkId >= 0 && checkId != datasetId){
//			return false;
//		}
//		if(checkId == datasetId){
//			return true;
//		}
		if(!CustomDataset.isThere(datasetId)){
			return false;
		}
		int groupID = CustomDataset.getGroupIdByCustomDatasetId(datasetId);
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn.prepareStatement("insert into custom_dataset_label(custom_dataset_id,group_id,name,lang) values(?,?,?,?) on duplicate key " +
					" update name=values(name)") ;// where custom_dataset_id=? and lang=?
			stmt.setInt(1, datasetId);
			stmt.setInt(2, groupID);
			stmt.setString(3, newName) ;
			stmt.setString(4, lang);
			stmt.executeUpdate();
			stmt.close();
			conn.close();
			CustomDataset.updateCustomDatasetDate(datasetId);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	@Deprecated
	public static boolean rename(int datasetId, String newName, int groupID){
		int checkId = getCustomDatasetIdByNameGroup(newName, groupID);
		if(checkId >= 0 && checkId != datasetId){
			return false;
		}
		if(checkId == datasetId){
			return true;
		}
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn.prepareStatement("update custom_dataset set name=? where id=?") ;
			stmt.setString(1, newName) ;
			stmt.setInt(2, datasetId) ;
			stmt.executeUpdate();
			stmt.close();
			conn.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public static void updateMembers(int datasetId, HashSet<String> members){
		if(!CustomDataset.isThere(datasetId)) return;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement rmpstmt = conn.prepareStatement("DELETE FROM custom_dataset_doc where dataset_id=?");
			rmpstmt.setInt(1, datasetId);
			rmpstmt.execute();
			rmpstmt.close();
			
			PreparedStatement mempstmt = conn.prepareStatement("INSERT INTO custom_dataset_doc(dataset_id,doc_id) values(?,?);");
			mempstmt.setInt(1, datasetId);
			for(String uri: members){
				int mid = URIIDCache.getInstance().getURIID(uri, true);
				if(mid>0){
					mempstmt.setInt(2, mid);
					mempstmt.executeUpdate();
				}
			}
			mempstmt.close();
			

			CustomDataset.updateCustomDatasetDate(datasetId);
			
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static CustomDataset getInstanceccc(int id) {
//		String name = getNameByCustomDatasetID(id);
		HashSet<String> urls = getURLsByCustomDatasetId(id);
		String lang = getLangByCustomDatasetId(id);
		String group = getGroupByCustomDatasetId(id);
		Date date = getCreatedDateByCustomDatasetID(id);
		int docNum = getDocNumByCustomDatasetId(id);
		return new CustomDataset(id, null, group, lang, urls, date, docNum);
	}

	public static boolean canEdit(int datasetId, int userGroupID){
		boolean can=false;
		String group = (String)CustomDataset.getGroupByCustomDatasetId(datasetId);
		int groupId = Group.getGroupIdByName(group);
		if(userGroupID == groupId){
			can = true;
		}
//		System.out.println("can:"+can);
		return can;
	}
	
	/*public static boolean canEdit(int datasetId, String user_name){
		boolean can=false;
		String group = (String)CustomDataset.getGroupByCustomDatasetId(datasetId);
		User user = new User(user_name);
		if(user.getGroups().contains(Group.getGroupIdByName(group))){
			can = true;
		}
		System.out.println("can:"+can);
		return can;
	}*/
	
	public static int getDocNumByCustomDatasetId(int customDatasetId) {
		// TODO Auto-generated method stub
		int num = 0;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery(String
							.format("SELECT count(*) FROM custom_dataset_doc where dataset_id = %d",
									customDatasetId));
			if (rs.next()) {
				num = rs.getInt(1);
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setURLs(HashSet<String> urls) {
		this.urls = urls;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

//	public void setAuthor(String author) {
//		this.author = author;
//	}
	public void setGroup(String group) {
		this.group = group;
	}

	public void setDocNum(int docNum) {
		this.docNum = docNum;
	}

	/**
	 * get the name of dataset
	 * 
	 * @param id
	 *            . the dataset id
	 * @return null if invalid dataset id
	 */
	public static ArrayList<DatasetName> getNameByCustomDatasetID(int id) {
		ArrayList<DatasetName> dnList = new ArrayList<DatasetName>();
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(String.format("select name "
//					+ "from custom_dataset " + "where id = %d", id));
//			if (rs.next()) {
//				name = rs.getString("name");
//			}
			ResultSet rs = stmt.executeQuery(String.format("select name,lang "
					+ "from custom_dataset_label " + "where custom_dataset_id = %d order by lang", id));
			String name = null;
			String lang = null;
			while (rs.next()) {
				name = rs.getString("name");
				lang = rs.getString("lang");
				dnList.add(new DatasetName(new DatasetItem(DatasetItem.CUSTOM_GENRE, id, null), name, lang));
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dnList;
	}
	public static void updateCustomDatasetDate(int datasetId){
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("update custom_dataset set update_time=now() where id=?");
			pstmt.setInt(1, datasetId);
			pstmt.execute();
			pstmt.close();
			pstmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static String getUpdateDateByCustomDatasetID(int id, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		String date = simpleDateFormat
				.format(getUpdateDateByCustomDatasetID(id));
		return date;
	}
	public static String getCreatedDateByCustomDatasetID(int id, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		String date = simpleDateFormat
				.format(getCreatedDateByCustomDatasetID(id));
		return date;
	}

	public static Date getUpdateDateByCustomDatasetID(int id) {
		Date date = null;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(String.format(
//					"SELECT time FROM custom_dataset where id = %d", id));
			ResultSet rs = stmt.executeQuery(String.format(
					"SELECT update_time FROM custom_dataset where id = %d", id));
			if (rs.next()) {
				date = rs.getDate("update_time");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getCreatedDateByCustomDatasetID(int id) {
		Date date = null;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(String.format(
//					"SELECT time FROM custom_dataset where id = %d", id));
			ResultSet rs = stmt.executeQuery(String.format(
					"SELECT create_time FROM custom_dataset where id = %d", id));
			if (rs.next()) {
				date = rs.getDate("create_time");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * get urls of the dataset!!!!!!!!!!!!!!!!!!
	 * 
	 * @return
	 */
	public HashSet<String> getURLs() {
		return urls;
	}

	public int getID() {
		return id;
	}

	public String getName() {
		return name;
	}

//	public String getAuthor() {
//		return author;
//	}
	public String getGroup() {
		return this.group;
	}

	public String getLang() {
		return lang;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public int getDocNum() {
		return docNum;
	}

	/**
	 * get the dataset's urls
	 * 
	 * @param customDatasetId
	 *            . the dataset id
	 * @return. the bookmark's URLs
	 */
	public static HashSet<String> getURLsByCustomDatasetId(int customDatasetId) {
		HashSet<String> result = new HashSet<String>();
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(String.format("select doc_id "
					+ "from custom_dataset_doc " + "where dataset_id = %d",
					customDatasetId));
			while (rs.next()) {
				try {
					result.add(URIIDCache.getInstance().getURI(
							rs.getInt("doc_id")));
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static ArrayList<Integer> getURLIDsByCustomDatasetId(
			int customDatasetId) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(String.format("select doc_id "
					+ "from custom_dataset_doc " + "where dataset_id = %d",
					customDatasetId )+ " order by doc_id;");
			while (rs.next()) {
				result.add(rs.getInt("doc_id"));
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static int getCustomDatasetIdByNameGroupLang(String name, String group, String lang) {
		int customDatasetId = -1;
//		int uid = User.getUserIdByName(author);
		int gid = Group.getGroupIdByName(group);
		if (gid < 0)
			return -1;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(String.format("select id "
//					+ "from custom_dataset "
//					+ "where name = '%s' and group_id = %d", name, gid));
			ResultSet rs = stmt.executeQuery(String.format("select id,custom_dataset_id "
					+ "from custom_dataset_label "
					+ "where name = '%s' and group_id = %d and lang='%s'", name, gid,lang));
			
			if (rs.next()) {
				customDatasetId = rs.getInt("custom_dataset_id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customDatasetId;
	}

	/**
	 * get dataset id by name with author
	 * 
	 * @param name
	 *            . the dataset name
	 * @param group
	 *            . the group name
	 * @return. -1 if the group has no dataset with the name
	 */
	@Deprecated
//	public static int getCustomDatasetIdByNameAuthor(String name, String author) {
	public static int getCustomDatasetIdByNameGroup(String name, String group) {
		int customDatasetId = -1;
//		int uid = User.getUserIdByName(author);
		int gid = Group.getGroupIdByName(group);
		if (gid < 0)
			return -1;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(String.format("select id "
					+ "from custom_dataset "
					+ "where name = '%s' and group_id = %d", name, gid));
			if (rs.next()) {
				customDatasetId = rs.getInt("id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customDatasetId;
	}

	public static int getCustomDatasetIdByNameGroupLang(String name, int groupID, String lang) {
		int customDatasetId = -1;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(String.format("select id "
//					+ "from custom_dataset "
//					+ "where name = '%s' and group_id = %d", name, groupID));
//			ResultSet rs = stmt.executeQuery(String.format("select id "
//					+ "from custom_dataset_label "
//					+ "where name = '%s' and group_id = %d", name, groupID));
			ResultSet rs = stmt.executeQuery(String.format("select id,custom_dataset_id "
					+ "from custom_dataset_label "
					+ "where name = '%s' and group_id = %d and lang='%s' ", name, groupID, lang));
			if (rs.next()) {
				customDatasetId = rs.getInt("custom_dataset_id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customDatasetId;
	}
	
	/**
	 * get dataset id by name with group
	 * 
	 * @param name
	 *            . the dataset name
	 * @param groupID
	 *            . the group ID
	 * @return. -1 if the group has no dataset with the name
	 */
	@Deprecated
	public static int getCustomDatasetIdByNameGroup(String name, int groupID) {
		int customDatasetId = -1;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(String.format("select id "
//					+ "from custom_dataset "
//					+ "where name = '%s' and group_id = %d", name, groupID));
			ResultSet rs = stmt.executeQuery(String.format("select id "
					+ "from custom_dataset_label "
					+ "where name = '%s' and group_id = %d", name, groupID));
			if (rs.next()) {
				customDatasetId = rs.getInt("id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customDatasetId;
	}

	/**
	 * get the dataset's lang
	 * 
	 * @param customId
	 *            . The dataset id
	 * @return the lang of dataset
	 */
	public static String getLangByCustomDatasetId(int customId) {
		String lang = null;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn
					.prepareStatement("select lang from custom_dataset where id=?");
			stmt.setInt(1, customId);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				lang = rs.getString("lang");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lang;
	}
	/**
	 * get the dataset's group
	 * 
	 * @param customId
	 *            . The dataset id
	 * @return the group of dataset
	 */
	public static int getGroupIdByCustomDatasetId(int customId) {
		int groupId = -1;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn
					.prepareStatement("select group_id from custom_dataset where id=?");
			stmt.setInt(1, customId);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				groupId = rs.getInt("group_id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return groupId;
	}
	/**
	 * get the dataset's group
	 * 
	 * @param customId
	 *            . The dataset id
	 * @return the group of dataset
	 */
	public static String getGroupByCustomDatasetId(int customId) {
		String group = null;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn
					.prepareStatement("select group_id from custom_dataset where id=?");
			PreparedStatement selps = conn
					.prepareStatement("SELECT name FROM edit_group where id = ?");
			stmt.setInt(1, customId);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				int user_id = rs.getInt("group_id");
				selps.setInt(1, user_id);
				ResultSet selrs = selps.executeQuery();
				if (selrs.next()) {
					group = selrs.getString(1);
				}
				selrs.close();
			}
			rs.close();
			stmt.close();
			selps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return group;
	}
	/**
	 * 判断以datasetID标识的自定义实体是否存在
	 * @param entityID
	 * @return
	 */
	public static boolean isThere(int datasetID){
		boolean result = false;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn.prepareStatement("select null from custom_dataset where id=?");
			stmt.setInt(1, datasetID) ;
			ResultSet rs = stmt.executeQuery();
			if ( rs.next() ) {
				result = true;
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 
	 * @param datasetId
	 * @param name
	 * @param gid
	 * @param lang
	 * @return true==conflict ; false== not conflict
	 */
	public static boolean checkNameConflict(int datasetId, String name, int gid, String lang){
		boolean conflict = true;
		int existId = getCustomDatasetIdByNameGroupLang(name, gid, lang);
		if(existId>0 && existId != datasetId){
			conflict = true;
		}else{
			conflict = false;
		}
		return conflict;
	}
	public boolean serializeNew(){
		int gid = Group.getGroupIdByName(group);
		if ( gid < 0 ) return false;
		boolean flag = false;
		int datsetId = getCustomDatasetIdByNameGroupLang(name, group,lang);
		if(datsetId>0)
			throw new IllegalArgumentException("The custom dataset has existed!");
		try {
			Connection conn = DBConnectionFactory.getConnection();
			String type = (this instanceof CustomDataset) ? DatasetItem.CUSTOM_GENRE : DatasetItem.URI_GENRE;

			PreparedStatement pstmt = conn.prepareStatement("insert into custom_dataset(group_id,create_time,update_time) values(?,now(),now()) ");
			pstmt.setInt(1, gid);
			pstmt.execute();
			ResultSet rs = pstmt.executeQuery("select last_insert_id() as id");
			if (rs.next()) {
				datsetId = rs.getInt("id");
			}
			rs.close();
			pstmt.close();
			conn.close();
			flag = addOrUpdateLangName(datsetId, this.name, this.lang);
			updateMembers(datsetId, this.urls);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.id = datsetId;
		return flag;
	}
	@Deprecated
	public boolean serialize(){
		int gid = Group.getGroupIdByName(group);
		if(gid < 0){
			return false;
		}
		boolean flag = false;
		int customDatasetId = getCustomDatasetIdByNameGroupLang(name, gid,lang);//getCustomDatasetIdByNameGroup(name, gid);
		if(customDatasetId >= 0){
			flag = true;
		}
		Connection conn = null;
		try {
			conn = DBConnectionFactory.getConnection();
//			conn.setAutoCommit(false);
//			conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);//=================
			if(!flag){
//				CustomDataset.rename(customDatasetId, gid, name, lang);
				PreparedStatement ipstmt = conn.prepareStatement("INSERT INTO custom_dataset(group_id,create_time,update_time,lang) values(?,now(),now(),?); select last_insert_id;" );
//						" insert into custom_dataset_label(custom_dataset_id,group_id,name,lang) values(?,?,?,?) on duplicate key set name=? ");
//				ipstmt.setString(1, name);
				ipstmt.setInt(1, gid);
				ipstmt.setString(2, lang);
				ipstmt.execute();
				
				ipstmt = conn.prepareStatement("select last_insert_id();");
				ResultSet rs = ipstmt.executeQuery();
				if(rs.next()){
					customDatasetId = rs.getInt(1);//=============
				}
				
				CustomDataset.rename(customDatasetId, name, gid);//.rename(customDatasetId, gid, name, lang);
				rs.close();
				ipstmt.close();
			}else{
				PreparedStatement upstmt = conn.prepareStatement("UPDATE custom_dataset SET update_time=now() WHERE id=?");
//				upstmt.setString(1, lang);
				upstmt.setInt(1, customDatasetId);
				upstmt.executeUpdate();
				upstmt.close();
//				PreparedStatement rmpstmt = conn.prepareStatement("DELETE FROM custom_dataset_doc where dataset_id=?");
//				rmpstmt.setInt(1, customDatasetId);
//				rmpstmt.execute();
//				rmpstmt.close();
			}
			PreparedStatement rmpstmt = conn.prepareStatement("DELETE FROM custom_dataset_doc where dataset_id=?");
			rmpstmt.setInt(1, customDatasetId);
			rmpstmt.execute();
			rmpstmt.close();
			
			PreparedStatement mempstmt = conn.prepareStatement("INSERT INTO custom_dataset_doc(dataset_id,doc_id) values(?,?);");
			mempstmt.setInt(1, customDatasetId);
			for(String uri: urls){
				int mid = URIIDCache.getInstance().getURIID(uri, true);
				if(mid>0){
					mempstmt.setInt(2, mid);
					mempstmt.executeUpdate();
				}
			}
			mempstmt.close();
			conn.commit();
//			conn.setAutoCommit(true);
			conn.close();
			flag = true;
		} catch (SQLException e) {
			flag = false;
//			if(conn != null){
//				try {
//					conn.rollback();
//					if(!conn.isClosed()){
//						conn.close();
//					}
//				} catch (SQLException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				System.out.println("Connection rollback....");
//			}
//			e.printStackTrace();
		} catch (Throwable e) {
			e.printStackTrace();
		} finally{
			try {
				if(conn != null && !conn.isClosed()){
					conn.close();
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return flag;
	}
	/**
	 * serialize the dataset, if the dataset name has existed, then merge the
	 * URLs
	 * 
	 * @return false if the group has dataset with the same name
	 */
	@Deprecated
	public boolean serialize_old() {
//		int uid = User.getUserIdByName(author);
		int gid = Group.getGroupIdByName(group);
		if (gid < 0)
			return false;
		boolean flag = false;
		int customDatasetId = getCustomDatasetIdByNameGroup(name, group);
		if (customDatasetId >= 0)
			flag = true;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			if (!flag) {
				Statement stmt = conn.createStatement();
				stmt.executeUpdate(String.format(
						"insert into custom_dataset(name, group_id, create_time, lang) "
								+ "values('%s', %d, now(), '%s')", name, gid,
						lang));
				stmt.close();

				customDatasetId = getCustomDatasetIdByNameGroup(name, group);
			}
			PreparedStatement pstmt = conn
					.prepareStatement("insert ignore into custom_dataset_doc(dataset_id, doc_id) "
							+ "values(?, ?)");
			pstmt.setInt(1, customDatasetId);
			for (String uri : urls) {
				pstmt.setInt(2,
						URIIDCache.getInstance().getURIID(uri, true));
				pstmt.executeUpdate();
			}
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * add an URL into the dataset
	 * 
	 * @param bookmarkId
	 * @param uriID
	 *            . the uri id
	 */
	public static void addURL(int customDatasetId, int uriID) {
		Connection conn = DBConnectionFactory.getConnection();
		try {
			PreparedStatement pstmt = conn
					.prepareStatement("insert ignore into custom_dataset_doc(dataset_id, doc_id) "
							+ "values(?, ?)");
			pstmt.setInt(1, customDatasetId);
			pstmt.setInt(2, uriID);
			pstmt.executeUpdate();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * delete an URL from the dataset
	 * 
	 * @param bookmarkId
	 * @param uriID
	 *            . the uri id
	 */
	public static void deleteURL(int bookmarkId, int uriID) {
		Connection conn = DBConnectionFactory.getConnection();
		try {
			PreparedStatement pstmt = conn
					.prepareStatement("delete from custom_dataset_doc where dataset_id=?  and doc_id=?");
			pstmt.setInt(1, bookmarkId);
			pstmt.setInt(2, uriID);
			pstmt.executeUpdate();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
